﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LANmine
{
    public partial class EditPasswordForm : Form
    {
        String user;
        private Language lmLang;
        public EditPasswordForm(string username, Language lmLang)
        {
            // save username & language object
            this.lmLang = lmLang;
            this.user = username;
            InitializeComponent();
            ApplyLanguage();
        }

        private void btnSavePass_Click(object sender, EventArgs e)
        {
            if (Login.AuthenticateUser(user, txtCurrentPass.Text))
            {
                lblCheckCurrentPass.Visible = false;
                if (txtNewPass.Text == txtNewPassConfirm.Text)
                {
                    lblCheckIfEqual.Visible = false;
                    ChangePass();
                    MessageBox.Show(this.lmLang.GetString("editpass.msgPassChanged"));
                    this.Close();
                }
                else
                {
                    lblCheckIfEqual.Visible = true;
                    lblCheckIfEqual.Text = this.lmLang.GetString("editpass.lblCheckIfEqual"); ;
                    lblCheckIfEqual.ForeColor = Color.Red;
                }
            }
            else
            {
                lblCheckCurrentPass.Visible = true;
                lblCheckCurrentPass.Text = this.lmLang.GetString("editpass.lblCheckCurrentPass"); ;
                lblCheckCurrentPass.ForeColor = Color.Red;
            }
        }
        
        private void ChangePass()
        {
            //MessageBox.Show("Paswoord zal gewijzigd worden");
            Database dbUsers = new Database("rw");
            SqlConnection dbConn = dbUsers.getDbConn();
            DataSet dsUsers = new DataSet();
            DataTable dtUsers = new DataTable("deelnemers");

            //open database connection to fetch data
            dbConn.Open();

            //Create data adapters and command builders 
            SqlDataAdapter daUsers = new SqlDataAdapter("SELECT TOP 1 * FROM deelnemers WHERE deelnemers.gebruikersnaam='" + user + "'", dbConn);
            SqlCommandBuilder cbUsers = new SqlCommandBuilder(daUsers);

            // Fill the datatable 
            daUsers.Fill(dtUsers);
            dsUsers.Tables.Add(dtUsers);

            // Close the DB connection
            dbConn.Close();

            // Modify DataSet 
            dsUsers.Tables["deelnemers"].Rows[0]["wachtwoord"] = Login.HashCode(txtNewPass.Text);

            //Open the DB connection to update 
            dbConn.Open();
            // Update the record
            daUsers.Update(dsUsers, "deelnemers");
            //Close the DB connection
            dbConn.Close();
 
        }

        #region Language Specifications
        /**
         * MainForm::ApplyLanguage()
         * ---------------------------
         * Overwrite all text properties of all elements in all forms with the right ones.
         */
        private void ApplyLanguage()
        {
            /**
             * Header label
             */
            this.lblEditPass.Text = this.lmLang.GetString("editpass.lblEditPass");
            /**
             * other labels & buttons
             */
            this.lblCurrentPass.Text = this.lmLang.GetString("editpass.lblCurrentPass");
            this.lblNewPass.Text = this.lmLang.GetString("editpass.lblNewPass");
            this.lblNewPassConfirm.Text = this.lmLang.GetString("editpass.lblNewPassConfirm");
            this.btnSavePass.Text = this.lmLang.GetString("editpass.btnSavePass");
        }
        #endregion
    }
}
